﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace WeatherService.Helpers
{
    public class IPHelper
    {

        public static IPAddress GetExternalIp()
        {
            string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp";
            WebClient wc = new WebClient();
            UTF8Encoding utf8 = new UTF8Encoding();
            string requestHtml = "";
            try
            {
                requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
            }
            catch (WebException we)
            {
                // do something with exception
                Console.Write(we.ToString());
            }

            //Set a Default incase service is down
            IPAddress externalIp = IPAddress.Parse("0.0.0.0");
            if (!String.IsNullOrEmpty(requestHtml))
            {
                externalIp = IPAddress.Parse(requestHtml);
            }
            return externalIp;
        }




    }
}
